package com.prs.crm.action.sale;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.domain.sale.SalesContractType;
import com.prs.crm.service.sale.SalesContractTypeService;

public class SalesContractTypeAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private SalesContractType  type;
	
	private Integer[] typeIds;
	
	private SalesContractTypeService salesContractTypeService;
	
	private PaginationSupport pagination;
	
	
	@Action("listSalesContractType")
	public String listSalesContractType()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(SalesContractType.class);
		this.setPagination(this.getSalesContractTypeService().listByPage(criteria,
							getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	
	@Action("editSalesContractType")
	public String editSalesContractType()
	{
		return SUCCESS;
	}
	
	@Action(value="saveSalesContractType",results={@Result(name="success",type="redirect",location="listSalesContractType"),
			@Result(name="error",type="chain",location="editSalesContractType")})
	public String saveSalesContractType() {
		if(type.getName() == null||type.getName().equals("")) {
			this.addActionError("合同类型名称不能为空");
			return ERROR;
		}
		for(SalesContractType contractType:this.getSalesContractTypeService().getAllType()){
			if(contractType.getName().trim().equals(type.getName().trim())){
				this.addActionError("合同类型名称已经存在");
				return ERROR;
			}
		}
		this.getSalesContractTypeService().save(type);
		return SUCCESS;
	}
	
	@Action(value="removeSalesContractType",results={@Result(name="success",type="redirect",location="listSalesContractType")})
	public String removeSalesContractType()
	{
		this.getSalesContractTypeService().batchRemove(typeIds);
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public SalesContractType getType() {
		return type;
	}

	public void setType(SalesContractType type) {
		this.type = type;
	}


	public SalesContractTypeService getSalesContractTypeService() {
		return salesContractTypeService;
	}

	public void setSalesContractTypeService(
			SalesContractTypeService salesContractTypeService) {
		this.salesContractTypeService = salesContractTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	
	
	

}
